﻿#include "UStateInfoPanel.h"
#include <QMessageBox>
#include <QBitmap>
UStateInfoPanel::UStateInfoPanel(QWidget *parent)
	: UBasePanel(parent)
{
	ui.setupUi(this);
	m_sNormalLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(120, 120, 120);";
	m_sWorkLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(0, 255, 0);";
	m_sAlarmLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255, 0, 0);";
	m_sWarningLamp = "min-width:20px;min-height:20px;"
		"max-width:20px;max-height:20px;"
		"border-radius:10px;border:1px solid black;"
		"background:rgb(255,255,0);";
	m_bIsStopButtonClicked = false;
	m_pAlarmDlg = nullptr;
	m_nVoiceCount = 0;
	m_nCount = 50;//初始化时将定时器循环次数设置成60
	//屏蔽回车键
	ui.pushButtonAlarm->setAutoDefault(false);
	ui.pushButtonEmergencyStop->setAutoDefault(false);
	//配置定时器
	m_pTimer = new QTimer(this);
	m_pTimer->setTimerType(Qt::TimerType::PreciseTimer);
	connect(m_pTimer, &QTimer::timeout, this, &UStateInfoPanel::slotDataRefresh);
	setStyleSheetFile();
	initState();//初始化界面状态
	initConnect();//初始化槽函数连接
}

UStateInfoPanel::~UStateInfoPanel()
{
	m_pTimer->stop();
	int nIndex = 0;
	while (m_pTimer->isActive())
	{
		if (nIndex > 500)
		{
			break;
		}
		nIndex++;
		QThread::msleep(10);
	}
	if (m_pAlarmDlg)
	{
		delete m_pAlarmDlg;
		m_pAlarmDlg = nullptr;
	}
	if (m_pTimer)
	{
		delete m_pTimer;
		m_pTimer = nullptr;
	}
}

void UStateInfoPanel::timerStart()
{
	m_pTimer->start(1000);
}

void UStateInfoPanel::init()
{
	m_pAlarmDlg = new CRobAlarmDlg(m_pRobCtrMange, m_pDevData);
	connect(m_pAlarmDlg, SIGNAL(sigAlarm(bool)), this, SLOT(slotAlarm(bool)));
	timerStart();
}

void UStateInfoPanel::setStyleSheetFile()
{
	/*this->setStyleSheet("QGroupBox{border: 1px solid black;margin-top: 2.3ex;border-radius: 10px;background-color: #888888;}"
		"QGroupBox::title{padding: 0 3px; font-size: 16px;subcontrol- origin:margin;left: 10px;}"
		"QLabel{background-color: transparent;border: 1px solid transparent;}"
	);*/
}

void UStateInfoPanel::initConnect()
{
	connect(ui.pushButtonEmergencyStop, &QPushButton::clicked, this, &UStateInfoPanel::slotPushButtonEmergencyStopClicked);
	connect(ui.pushButtonAlarm, &QPushButton::clicked, this, &UStateInfoPanel::slotPushButtonAlarmClicked);
}

void UStateInfoPanel::initState()
{
	setEmergencyStopUp();
}
void UStateInfoPanel::setRunningStateLableSheet(EState enState)
{
	if (enState == EState::enNormal)
	{
		ui.labelRunningState->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.labelRunningState->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.labelRunningState->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.labelRunningState->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setCommunicationStateLableSheet(EState enState)
{
	if (enState == EState::enNormal)
	{
		ui.labelCommunicationState->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.labelCommunicationState->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.labelCommunicationState->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.labelCommunicationState->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setAlarmLableSheet(EState enState)
{
	
	if (enState == EState::enNormal)
	{
		ui.pushButtonAlarm->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.pushButtonAlarm->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.pushButtonAlarm->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.pushButtonAlarm->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setChargStateLableSheet(EState enState)
{
	if (enState == EState::enNormal)
	{
		ui.labelChargState->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.labelChargState->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.labelChargState->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.labelChargState->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setRobStateLableSheet(EState enState)
{
	if (enState == EState::enNormal)
	{
		ui.labelRobState->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.labelRobState->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.labelRobState->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.labelRobState->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setYTStateLableSheet(EState enState)
{
	if (enState == EState::enNormal)
	{
		ui.labelYTState->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.labelYTState->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.labelYTState->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.labelYTState->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setBeltALableSheet(EState enState)
{
	if (enState == EState::enNormal)
	{
		ui.labelBeltA->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.labelBeltA->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.labelBeltA->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.labelBeltA->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setBeltBLableSheet(EState enState)
{
	if (enState == EState::enNormal)
	{
		ui.labelBeltB->setStyleSheet(m_sNormalLamp);
	}
	else if (enState == EState::enWorking)
	{
		ui.labelBeltB->setStyleSheet(m_sWorkLamp);
	}
	else if (enState == EState::enWarning)
	{
		ui.labelBeltB->setStyleSheet(m_sWarningLamp);
	}
	else if (enState == EState::enAlarm)
	{
		ui.labelBeltB->setStyleSheet(m_sAlarmLamp);
	}
}

void UStateInfoPanel::setEmergencyStopDown()
{
	//QPixmap pix(":/icon/Rob/press.png");
	//ui.pushButtonEmergencyStop->setStyleSheet("border-image: url(:/icon/Rob/press.png);");
	QPixmap pix(":/icon/Rob/pressNew.png");
	ui.pushButtonEmergencyStop->setStyleSheet("border-image: url(:/icon/Rob/pressNew.png);");
	//ui.pushButtonEmergencyStop->setMask(pix.mask());
}

void UStateInfoPanel::setEmergencyStopUp()
{
	//QPixmap pix(":/icon/Rob/notPress.png");
	//ui.pushButtonEmergencyStop->setStyleSheet("border-image: url(:/icon/Rob/notPress.png);");
	QPixmap pix(":/icon/Rob/notPressNew.png");
	ui.pushButtonEmergencyStop->setStyleSheet("border-image: url(:/icon/Rob/notPressNew.png);");
	//ui.pushButtonEmergencyStop->setMask(pix.mask());
}

void UStateInfoPanel::slotPushButtonEmergencyStopClicked()
{
	if (false == m_bIsStopButtonClicked)
	{
		SDevExecLog sLog;
		sLog.m_dLogTime = QDateTime::currentDateTime();
		sLog.m_nAlarmLevel = enWorkInfo;
		sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
			.arg("admin")
			.arg(m_pDevData->m_sRobName)
			.arg(A2T("急停"));
		sLog.m_sDevName = A2T("机器人远程操控");
		m_pDevData->addDevExecLog(sLog);
		//按下急停
		SCtrInfo stCtrInfo;
		stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
		stCtrInfo.m_bCtrSync = true;//急停要同步
		stCtrInfo.m_nCtrCmd = ECCmdType::enCSuddenStop;
		m_pRobCtrMange->ctrDevice(stCtrInfo);
		m_pDevData->m_bSuddenStop = true;
		//将本界面中的UI做出相应的变化
		setEmergencyStopDown();
		m_bIsStopButtonClicked = true;
		//禁用所有按钮
		//emit(sigDisEnableOperateButton(1));//通知外层界面禁用等级为1的按钮
	}
	else
	{
		//提醒一下是否确定松开急停
		int nRet = messageBox(3, A2T("是否确定松开急停"));
		if (QMessageBox::No == nRet || QMessageBox::Close == nRet)
		{
			return;
		}
		SDevExecLog sLog;
		sLog.m_dLogTime = QDateTime::currentDateTime();
		sLog.m_nAlarmLevel = enWorkInfo;
		sLog.m_sDescriptionl = A2T("%1点击了%2的%3按钮")
			.arg("admin")
			.arg(m_pDevData->m_sRobName)
			.arg(A2T("松开急停"));
		sLog.m_sDevName = A2T("机器人远程操控");
		m_pDevData->addDevExecLog(sLog);
		SCtrInfo stCtrInfo;
		stCtrInfo.m_sGuid = m_pDevData->m_sDevGuid;
		stCtrInfo.m_bCtrSync = true;//急停要同步
		stCtrInfo.m_nCtrCmd = ECCmdType::enCNoCmd;
		m_pRobCtrMange->ctrDevice(stCtrInfo);
		m_pDevData->m_bSuddenStop = false;
		//将本界面中的UI做出相应的变化
		setEmergencyStopUp();
		m_bIsStopButtonClicked = false;
		//恢复所有按钮
		//emit(sigEnableOperateButton(1));//通知外层界面恢复禁用等级为1的按钮
	}
}

void UStateInfoPanel::slotPushButtonAlarmClicked()
{
	m_pAlarmDlg->exec();
}

void UStateInfoPanel::slotDataRefresh()
{

	if (!isVisible())
	{
		return;
	}
	m_pDevData->m_pMutex->lock();
	//通信状态
	bool bConnectState = m_pDevData->m_bPlcConnectState;
	if (bConnectState == false)
	{
		setCommunicationStateLableSheet(enAlarm);
		setRobStateLableSheet(enAlarm);
	}
	else
	{
		setCommunicationStateLableSheet(enWorking);
		setRobStateLableSheet(enWorking);
	}

	//运行状态三色灯状态(0:熄灭 1:黄灯 2:绿灯 3:红灯 4:绿闪)
	int nRunState = m_pDevData->m_nLightStatus;
	if (nRunState == 0)
	{
		setRunningStateLableSheet(enNormal);
	}
	else if (nRunState == 1)
	{
		setRunningStateLableSheet(enWarning);
	}
	else if (nRunState == 2)
	{
		setRunningStateLableSheet(enWorking);
	}
	else if (nRunState == 3)
	{
		setRunningStateLableSheet(enAlarm);
	}
	else if (nRunState == 4)
	{
		setRunningStateLableSheet(enWorking);
	}
	else
	{
		setRunningStateLableSheet(enNormal);
	}

	//充电状态
	if (m_pDevData->m_sDevWorkStatus.toInt() == 3)
	{
		setChargStateLableSheet(enWorking);
	}
	else
	{
		setChargStateLableSheet(enNormal);
	}

	//报警状态
	if (m_bIsAlarm)
	{
		setAlarmLableSheet(enAlarm);
	}
	else
	{
		setAlarmLableSheet(enWorking);
	}
	m_pDevData->m_pMutex->unlock();
	//云台状态隔1分钟ping一下，不用一秒钟ping一次
	if (m_pDevData->m_pDevDataCam->m_bLogin == true)
	{
		setYTStateLableSheet(enWorking);
	}
	else//登录一下
	{
		if (m_nCount == 60)
		{
			SCtrInfo info;
			info.m_nCtrCmd = enCLogin;
			info.m_bCtrSync = false;
			info.m_bIsLog = false;
			m_pCamerCtrMange->ctrDevice(info);
			m_nCount = 0;
		}
		else
		{
			m_nCount++;
		}
		setYTStateLableSheet(enAlarm);
	}

	//处理皮带启停
	if (m_pDevData->m_bBeltAWork)
	{
		setBeltALableSheet(enWorking);
	}
	else
	{
		setBeltALableSheet(enNormal);
	}
	if (m_pDevData->m_bBeltBWork)
	{
		setBeltBLableSheet(enWorking);
	}
	else
	{
		setBeltBLableSheet(enNormal);
	}
	
	m_pDevData->m_pMutex->lock();
	//处理播报
	double fSpeed = m_pDevData->m_sRobCurrSpeed.toDouble();             //当前速度
	m_pDevData->m_pMutex->unlock();
	if (fSpeed > 1000)
	{
		if (m_nVoiceCount == m_pDevData->m_pDevDataCam->m_nBroadcastTime)//播报一下机器人正在巡检
		{
			if (m_pRobCtrMange != nullptr)
			{
				SCtrInfo info;
				info.m_nCtrCmd = enBroadcastAudio;
				info.m_bCtrSync = false;
				info.m_bIsLog = false;
				m_pCamerCtrMange->ctrDevice(info);
				m_nVoiceCount = 0;	
			}
		}
		else
		{
			m_nVoiceCount++;
		}
	}
	else
	{
		m_nVoiceCount = 0;
	}

}

void UStateInfoPanel::slotAlarm(bool bIsAlarm)
{
	m_bIsAlarm = bIsAlarm;
}